package com.jia.leetCode;

/**
 * @program: Leetcode
 * @description: 213.打家劫舍II
 * @author: STU756
 * @create: 2020-08-05 13:26
 */
public class Pro0213 {
    //一维 dp[i] = max{dp[i-2] + nums[i], dp[i-1]}
    //把环分为两种情况
    //一个是0到n-1，另一个是从1到n,然后返回最大值
    public int rob2(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        if(nums.length == 1) return nums[0];
        int n = nums.length;
        int[] dp1 = new int[n];
        int[] dp2 = new int[n];
        dp1[0] = nums[0];
        dp1[1] = Math.max(dp1[0], nums[1]);
        for(int i = 2; i < nums.length-1; i++) {
            dp1[i] = Math.max(dp1[i-2] + nums[i], dp1[i-1]);
        }
        dp2[0] = 0;
        dp2[1] = nums[1];
        for(int i = 2; i<nums.length; i++) {
            dp2[i] = Math.max(dp2[i-2] + nums[i], dp2[i-1]);
        }
        return Math.max(dp1[n - 2], dp2[n - 1]);
    }
}
